package com.android.jay.wzp_voice.friend;

import android.content.Context;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.jay.lib_commin_ui.recyclerview.wrapper.LoadMoreWrapper;
import com.android.jay.lib_network.okhttp.response.listener.DisposeDataListener;
import com.android.jay.wzp_voice.R;
import com.android.jay.wzp_voice.api.RequestCenter;
import com.android.jay.wzp_voice.friend.adapter.FriendRecyclerAdapter;
import com.android.jay.wzp_voice.model.friend.BaseFriendModel;
import com.android.jay.wzp_voice.model.friend.FriendBodyValue;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

/**
 * Created by wzp on 2019/11/4 0004.
 * <p>
 * 描述  ：首页朋友fragment
 */
public class FriendFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, LoadMoreWrapper.OnLoadMoreListener {

    private Context mContext;
    /**
     * UI
     **/
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRecyclerView;

    private FriendRecyclerAdapter mAdapter;

    private LoadMoreWrapper mLoadMoreWrapper;

    /**
     * data
     **/

    private BaseFriendModel mRecommandData;
    private List<FriendBodyValue> mDatas = new ArrayList<>();


    public static Fragment newInstance() {
        FriendFragment fragment = new FriendFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container
            , @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_friend_layout, null);
        mSwipeRefreshLayout = view.findViewById(R.id.refresh_layout);
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(android.R.color.holo_red_light));
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView = view.findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //更新UI
        requestData();
    }


    @Override
    public void onRefresh() {
        requestData();
    }

    //请求数据
    private void requestData() {

        RequestCenter.requestFriendData(new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                //显示数据
                mRecommandData = (BaseFriendModel) responseObj;
                Log.e("kkkkkkk","reeeee"+((BaseFriendModel) responseObj).data);
                updateUI();

            }

            @Override
            public void onFailure(Object reasonObj) {
                //显示错误页面

            }
        });

    }

    private void updateUI() {
        mSwipeRefreshLayout.setRefreshing(false);
        mDatas = mRecommandData.data.list;
        mAdapter = new FriendRecyclerAdapter(mContext, mDatas);
        mLoadMoreWrapper = new LoadMoreWrapper(mAdapter);
        mLoadMoreWrapper.setOnLoadMoreListener(this);
        mLoadMoreWrapper.setLoadMoreView(R.layout.default_loading);
        mRecyclerView.setAdapter(mLoadMoreWrapper);
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void loadMore() {
        RequestCenter.requestFriendData(new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                //显示数据
                BaseFriendModel mMoreData = (BaseFriendModel) responseObj;
                mDatas.addAll(mMoreData.data.list);
                mLoadMoreWrapper.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Object reasonObj) {
                //显示错误页面

            }
        });
    }
}
